import smtplib
import chardet
import json
import logging
import inspect
from datetime import datetime

def sendEmail(detail=''):
    import smtplib
    from email.mime.text import MIMEText
    from email.header import Header

    # 设置发件人、收件人和邮件内容
    from_email = 'sdw035@outlook.com'
    to_email = 'sdw035@outlook.com'
    sys_email_auth = "sdwsdw123"

    now = datetime.now()
    formatted_time = now.strftime("%Y-%m-%d %H:%M:%S")
    mail_content = f'BD-快排反馈(服务)邮件,时间: {formatted_time}\n\n{detail}'

    # 创建邮件
    msg = MIMEText(mail_content)
    msg['From'] = Header(from_email)
    msg['To'] = Header(to_email)
    msg['Subject'] = Header('BD-快排邮件')

    # 发送邮件
    try:
        server = smtplib.SMTP_SSL('smtp.qq.com', 465)
        server.login(from_email, sys_email_auth)
        server.sendmail(from_email, [to_email], msg.as_string())
        server.quit()

        current_frame = inspect.currentframe()  # 当前行号
        logging.info("debugger sendmail, line {} >>>".format(current_frame.f_lineno))
        logging.info("\n send Email current_time : {} ".format(formatted_time))
        logging.info("\n to_email {} sent successfully.".format(to_email))
        logging.info("\n mail_content {}.\n".format(mail_content))

    except smtplib.SMTPException as e:
        print('Error occurred:', e)
        current_frame = inspect.currentframe()  # 当前行号
        logging.info("debugger sendmail error, line {} >>>".format(current_frame.f_lineno))
        logging.info("\n" + str(e))

    response_dict = {
        "status": 'success',
        "code": 0,
        "message":  "感谢你的反馈，我在收到邮件第一时间会回复您!"
    }

    return response_dict
